  function [hnew,flag_nopd, xh]=hessian_one( par0 , indmat , h )
% ================================================================
% function [hnew,flag_nopd, xh]=hessian_one( par0 , indmat , h ) 
% 
% Compute Hessian from csminwel using the delta-method 
% 
% Inputs 
% ------
% h         hessian from csminwel 
% par0      model coefficient around which hessian computed
%           par0 is transformed to xh for the computation of the Hessian
% flag_nopd == 1 if hnew not pd 
%           == 0 i pd 
% 
% xh       par0( modtomin, indmat ) 
% 
% Giorgio Primiceri and Alejandro Justiniano 4/4/2005 
% 4/11/05 Changes check pd
% ==============================================================
np=size(par0); 
dim=size(indmat); 
if dim(1)~=np 
    error('Rows of indmat must match number of coefficients' ) 
end 
if dim(2)~=4 
    error('indmat must have four columns') 
end 
clear dim; 
if any(imag(par0)~=0)==1 
    error('Input par0 has imaginary entries') 
end 
chone = find( indmat(:,1)==1 ); 
if isempty( chone ) == 0 
    lb = indmat( chone,2 ); 
    ub = indmat( chone,3 ); 
    if any( par0( chone ) < lb ) ==1 | any( par0(chone) > ub ) == 1 
        error('par0 not contained within bounds of indmat') 
    end 
end 
clear ch*; 

% Check  
xh=mod2min(par0,indmat); 
if any( imag( xh ) ~= 0 ) == 1 
    error('Model to Min transformation produces complex value') 
end 
disp('  '); 
disp('Delta Method Hessian'); 
disp('  '); 
% ---------------
% Begian Hessian 
% --------------
GG=zeros( np ); 
ii=1; 
for ii=1:np 
    if indmat(ii,1) == 1
        bb=indmat(ii,3); 
        aa=indmat(ii,2); 
        gdiv = 0.5*( bb - aa )/ ( ( 1 + xh(ii)^2 )^(3/2) ); 
    elseif indmat(ii,1) == 2
        bb=indmat(ii,3); 
        gdiv = exp( xh(ii) -bb ); 
    end
    GG(ii,ii) = gdiv; 
end 
hnew=GG*h*GG'; 
hnew=( hnew + hnew' )/2;
[junk,temp] = chol( hnew ) ; 
if  temp~=0   
    flag_nopd = 1;
    disp('Delta-Method Hessian is not positive definite');
else 
    flag_nopd = 0; 
end 